package be.phl.obstacleapp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import be.phl.model.DBAdapterPatient;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PatientFragment extends Fragment {

	private static final String TAG = "PatientFragment";
	Button btnSave;
	TextView txtName, txtAdress, txtN, txtA, txtE, txtT;
	EditText txtEmail, txtNumber;
	private int PatientId;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstances) {
		return inflater.inflate(R.layout.fragment_patient, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		txtName = (TextView) getActivity().findViewById(R.id.txtName);
		txtAdress = (TextView) getActivity().findViewById(R.id.txtAdress);
		txtEmail = (EditText) getActivity().findViewById(R.id.txtEmail);
		txtNumber = (EditText) getActivity().findViewById(R.id.txtNumber);
		btnSave = (Button) getActivity().findViewById(R.id.btnSave);
		txtN = (TextView) getActivity().findViewById(R.id.txtN);
		txtA = (TextView) getActivity().findViewById(R.id.txtA);
		txtE = (TextView) getActivity().findViewById(R.id.txtE);
		txtT = (TextView) getActivity().findViewById(R.id.txtT);

		Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
				"com.ttf");

		txtN.setTypeface(tf, Typeface.BOLD);
		txtA.setTypeface(tf, Typeface.BOLD);
		txtE.setTypeface(tf, Typeface.BOLD);
		txtT.setTypeface(tf, Typeface.BOLD);
		btnSave.setTypeface(tf, Typeface.BOLD);

		btnSave.setOnClickListener(btnOnClickListener);

		PatientId = getActivity().getIntent().getIntExtra("PatientId", 0);
		DBAdapterPatient db = new DBAdapterPatient(getActivity());

		db.open();
		Cursor p = db.getPatient(PatientId);

		while (p.moveToNext()) {
			
			getActivity().setTitle(p.getString(1) + " " + p.getString(2));
			
			txtName.setText(p.getString(1) + " " + p.getString(2));
			txtEmail.setText(p.getString(3));
			txtNumber.setText(p.getString(4));

			Cursor b = db.getBuilding(p.getInt(5));
			while (b.moveToNext()) {
				txtAdress.setText(b.getString(1) + " " + b.getString(2));
			}
		}
		db.close();
	}

	Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			Pattern p = Pattern.compile("([0-9]*)");
			Matcher m = p.matcher(txtNumber.getText().toString());
			View focusView = null;

			if (txtEmail.getText().toString().equals("")) {
				txtEmail.setError("Mag niet leeg zijn");
				txtEmail.requestFocus();
			} else if (!android.util.Patterns.EMAIL_ADDRESS.matcher(
					txtEmail.getText().toString()).matches()) {
				txtEmail.setError("Geen correct email-adres");
				txtEmail.requestFocus();
			} else if (!m.matches()) {
				txtNumber.setError("Mag enkel uit nummers bestaan");
				txtNumber.requestFocus();
			} else if (txtNumber.getText().toString().equals("")) {
				txtNumber.setError("Mag niet leeg zijn");
				txtNumber.requestFocus();
			}

			else {
				PatientId = (int) getActivity().getIntent().getIntExtra(
						"PatientId", 0);
				DBAdapterPatient db = new DBAdapterPatient(getActivity());
				db.open();

				if (db.updatePatient(PatientId, txtEmail.getText().toString(),
						txtNumber.getText().toString()) > 0) {
					Context context = getActivity().getApplicationContext();
					CharSequence text = "Aanpassen succesvol!";
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				} else {
					Context context = getActivity().getApplicationContext();
					CharSequence text = "Aanpassen mislukt!";
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}

				db.close();
			}
		}
	};
}
